# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from application_ui import Ui_ApplicationPage
from gui_ui import Ui_GuiPage

class ApplicationPage(QWizardPage):
    def __init__(self, parent=None):
        super(ApplicationPage, self).__init__(parent)
        self.ui = Ui_ApplicationPage()
        self.ui.setupUi(self)
        self.ui.btnBrowse.clicked.connect(self.browse)
        self.registerField("appName*", self.ui.editApplicationName)
        self.registerField("appVersion", self.ui.editApplicationVersion)
        self.registerField("orgName*", self.ui.editOrganizationName)
        self.registerField("location", self.ui.editLocation)
        self.registerField("encoding", self.ui.editEncoding)
        
        d = QDesktopServices.storageLocation(QDesktopServices.DocumentsLocation)
        self.ui.editLocation.setText(d)
        
    def browse(self):
        initial_dir = self.ui.editLocation.text()
        d = QFileDialog.getExistingDirectory(self, "Browse", initial_dir)
        if not d.isEmpty():
            self.ui.editLocation.setText(d)
            
            
            
class GuiPage(QWizardPage):
    def __init__(self, parent=None):
        super(GuiPage, self).__init__(parent)
        self.ui = Ui_GuiPage()
        self.ui.setupUi(self)
        self.registerField("uiFilename*", self.ui.editUiFilename)
        self.registerField("uiClassname*", self.ui.editUiClassname)
        self.registerField("mainWindowClassname*", self.ui.editMainWindowClassname)
        self.registerField("useSettings", self.ui.cbxSettings)
        self.registerField("keepState", self.ui.cbxState)
        
    def initializePage(self):
        appName = self.field("appName").toString()
        #todo: sanitize appName (spaces to CamelCase etc.)
        ui_file = appName.toLower() + "_ui"
        ui_class = "Ui_" + appName
        window_class = appName + "Window"
        self.ui.editUiFilename.setText(ui_file)
        self.ui.editUiClassname.setText(ui_class)
        self.ui.editMainWindowClassname.setText(window_class)
        
        
class ConclusionPage(QWizardPage):
    def __init__(self, parent=None):
        super(ConclusionPage, self).__init__(parent)
        self.lblMessage = QLabel(self)
        self.lblMessage.setAlignment(Qt.AlignLeft | Qt.AlignTop)
        layout = QVBoxLayout()
        layout.addWidget(self.lblMessage)
        self.setLayout(layout)
        self.setTitle("Conclusion")
        
    def initializePage(self):
        appName = self.field("appName").toString()
        appVersion = self.field("appVersion").toString()
        orgName = self.field("orgName").toString()
        location = self.field("location").toString()
        uiFile = self.field("uiFilename").toString()
        uiClass = self.field("uiClassname").toString()
        winClass = self.field("mainWindowClassname").toString()
        useSettings = self.field("useSettings").toBool()
        keepState = self.field("keepState").toBool()
        message = QString("""
Generate application skeleton for: %1 %2
Organization: %3        
Files created in: %4
UI filename: %5
UI class: %6
Main window class: %7
Use settings: %8
Keep state: %9
""").arg(appName).arg(appVersion).arg(orgName).arg(location).arg(uiFile).arg(uiClass).arg(winClass).arg(useSettings).arg(keepState)

        self.lblMessage.setText(message)